<?php
/*
	提现
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
global $_RQ;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return=array();
$return['ischecklogin']=cfc('core')->checklogin('','',true);
$setting=uni_setting('cash');
$open=model('core/member/cash')->check_open();
$openred=model('core/member/cash')->check_open(true);
if(!$open && !$openred && !$setting['openjifen']){
	exi('系统不支持提现！','','goback');
}
if($this->setting['is_huiqi365']){
	$item=model('jy_huiqi365/model')->check_qianyue($_SESSION['uid'],$this->setting['is_mobileshi']);
	if(!$item['passed']){
		$_SESSION['forward_url']='jy_cun9fan/index/';
		exi('','','jy_huiqi365/index/');
	}elseif($item['passed']==2){
		exi('您的签约还在审核中！不能提现！','error','jy_cun9fan/index/');
	}
}
$setting['percent']=round($setting['percent'],2)*1;
$setting['jifen_rate']=$setting['jifen_rate']?:1;
$setting['jifen_rate']=round($setting['jifen_rate'],2)*1;
$setting['desc'] = str_replace("\n","</br>",$setting['desc']);
if($_RQ['op']=='get'){
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','notixian','mobile','paypassword'));
	if($member['notixian']){
		exi('您暂时不支持提现！','','goback');
	}
	if($setting['max_tinum']){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$isnum=pdo_count('core_member_cash',array('createtime >'=>$daytime,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if($isnum>=$setting['max_tinum']){
			exi('每天只能提现'.$setting['max_tinum'].'次！明天再来吧！','','goback');
		}
	}
	$member['ideal_money']=pdo_getcolumn('core_ideal_money',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'money');
	$member['ideal_money']=$member['ideal_money']?$member['ideal_money']:'0.00';
	$return['item']=$member;
	$return['item']['banklist']=array();
	$type_array=array();
	$wxapppay_types=array();
	$check=strstr($setting['withdrawtype'],'0');
	if($check){
		$type_array[0]='提现到微信钱包';
		$wxapppay_types[]=array(
				'key'=>'0',
				'title'=>'提现到微信钱包',
		);
	}
	$check=strstr($setting['withdrawtype'],'1');
	if($check){
		$type_array[1]='提现到支付宝';
		$wxapppay_types[]=array(
				'key'=>'1',
				'title'=>'提现到支付宝',
		);
        $post1=pdo_get('core_member_cash',array('type'=>1,'mid'=>$_SESSION['uid']),array('alipay','realname'),'id desc');
	}
	$check=strstr($setting['withdrawtype'],'2');
	if($check){
		$type_array[2]='提现到银行卡';
		$wxapppay_types[]=array(
				'key'=>'2',
				'title'=>'提现到银行卡',
		);
        $post2=pdo_get('core_member_cash',array('type'=>2,'mid'=>$_SESSION['uid']),array('bankname','bankcard','realname','mobile','codesn'),'id desc');
        $post2['bankcard1']=$post2['bankcard'];
	}
	$check=strstr($setting['withdrawtype'],'api');
	if($check){
		$type_array['api']='接口提现';
		$wxapppay_types[]=array(
			'key'=>'api',
			'title'=>'接口提现',
		);
		$post2=pdo_get('core_member_cash',array('type'=>'api','mid'=>$_SESSION['uid']),array('bankname','bankcard','realname','mobile'),'id desc');
        $post2['bankcard1']=$post2['bankcard'];
	}
    if($post1 || $post2){
		$post1=$post1?:array();
		$post2=$post2?:array();
        $return['post'] = array_merge($post1, $post2);
    }
	if($this->setting['is_huiqi365']){
		$return['post']['realname']=$item['realname'];
	}
	$setting['type_array']=$type_array;
	$return['wxapppay_types']=$wxapppay_types;
	$return['setting']=$setting;
	if(!$open){
		$return['post']['fee_type']=1;
	}else{
		$return['post']['fee_type']=0;
	}
	$return['post']['fee_type']=$_RQ['fee_type']?:$return['post']['fee_type'];
    $return['post']['type']='no';
    if($_SESSION['wxapp_id'] && !$_SESSION['fromh5wxapp']){
        $return['post']['type']='1';
    }

	$setting=uni_setting('register');
	$iscode=$setting['dxmb'] && $setting['login_type']?1:0;
	$return['post']['iscode']=$iscode;
	$return['post']['is_mobileshi']=$this->setting['is_mobileshi']?:0;
	//接口提现
	if($this->setting['is_bankopen']){
		$apilog=model('core/member/cash')->getlog('',$this->setting['is_mobileshi']);
		$return['post']['bankid']=$apilog['id'];
		$return['post']['isapi']=1;
		$return['post']['iscodesn']=1;
		if($apilog['passed']==1){
			$return['post']['isapibank']=1;
			$return['post']['realname']=$apilog['realname'];
			$return['post']['mobile']=$apilog['mobile'];
			$return['post']['bankname']=$apilog['bankname'];
			$return['post']['bankcard']=$apilog['bankcard'];
		}
	}
	exi($return);
}
if($_RQ['op']=='post'){
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid']));
	$post=get_postdata();
	$fee=$post['fee'];
	$isnobei= fmod($fee,100);
	if($setting['is_ti100'] && $isnobei>0){
		exi('提现额度必须是100的倍数！','error');
	}
	if($post['type']=='no'){
		exi('请选择提现方式！','error');
	}
	if(!$_SESSION['wxapp_id'] && (!$post['password'] || $post['password']!=$member['paypassword'])){
		exi('支付密码不正确！','error');
	}
	if($post['type']==1){
		if(!$post['realname']){
			exi('请输入姓名','error');
		}
		if(!$post['alipay']){
			exi('请输入支付宝帐号','error');
		}
		if(!$post['alipay1']){
			exi('请确认帐号','error');
		}
		if($post['alipay1']!=$post['alipay']){
			exi('确认帐号与支付宝帐号不匹配！','error');
		}
	}
	if($post['type']==2 || $post['type']=='api'){
		if(!$post['realname']){
			exi('请输入姓名','error');
		}
		if(!$post['codesn'] && $post['iscodesn']){
			exi('请输入身份证号','error');
		}
		if(!$post['bankname'] && !$post['isapibank']){
			exi('请输入所属银行','error');
		}
		if(!$post['bankcard']){
			exi('请银行卡号','error');
		}
		if(!$post['bankcard1'] && !$post['isapibank']){
			exi('请确认卡号','error');
		}
		if($post['bankcard']!=$post['bankcard1'] && !$post['isapibank']){
			exi('确认卡号与银行卡号不匹配！','error');
		}
	}
	model('core/member/cash')->check_cash_fee($_SESSION['uid'],$fee);
	$cash=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'ordersn'=>get_ordersn('core_member_cash','ordersn','CASH'),
		'money'=>$fee,
		'type'=>intval($post['type']),
		'realname'=>$post['realname'],
		'codesn'=>$post['codesn'],
		'alipay'=>$post['alipay'],
		'bankname'=>$post['bankname'],
		'bankcard'=>$post['bankcard'],
		'mobile'=>$post['mobile'],
		'service_money'=>round($fee*$setting['percent']/100,2),
		'createtime'=>TIMESTAMP,
		'status'=>0,
	);
	$cash['cash_money']=$cash['money']-$cash['service_money'];
	pdo_insert('core_member_cash',$cash);
	$id=pdo_insertid();
	$type_array=array('提现到微信钱包','提现到支付宝','提现到银行卡');
	$typetitle=$type_array[$cash['type']];
	model('core/member/cash')->notice_tixian($_SESSION['uid'],$fee,$typetitle);
	if($setting['check'] || $cash['type'] !=0){
		exi('申请提现成功，请等待管理员审核！','','goback');
	}else{
		model('core/member/cash')->pay($id);
		exi('提现成功！','success','goback');
	}
}
if($_RQ['op']=='redpost'){
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid']));
	$post=get_postdata();
	if(!$post['password'] || $post['password']!=$member['paypassword']){
		exi('支付密码不正确！','erroe');
	}
	if(!$post['fee']){
		exi('请输入金额','error');
	}
	if(!$post['to_type']){
		exi('请选择提现方式','error');
	}
	$fee=$post['fee'];
	if(is_numeric($fee)){
	}else{
		exi('提现红包必须是数字！','error');
	}
	$service_money=round($fee*$setting['percent']/100,2);
	$service_money=intval($post['to_type'])==1?0:$service_money;
	$params=array(
		'mid'=>$_SESSION['uid'],
		'to_type'=>intval($post['to_type']),
		'realname'=>$post['realname'],
		'alipay'=>$post['alipay'],
		'bankname'=>$post['bankname'],
		'bankcard'=>$post['bankcard'],
		'money'=>$fee,
		'cash_money'=>$fee-$service_money
	);
	$cash_id=model('core/red_pack')->cash($params);
	if($setting['check'] || $params['to_type'] !=0){
		exi('申请提现成功，请等待管理员审核！','','goback');
	}else{
		model('core/red_pack')->pay_cash($cash_id);
		exi('提现成功！','success','goback');
	}
}
if($_RQ['op']=='jifenpost'){
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid']));
	$post=get_postdata();
	if($post['type']=='no'){
		exi('请选择提现方式！','erroe');
	}
	if(!$post['password'] || $post['password']!=$member['paypassword']){
		exi('支付密码不正确！','erroe');
	}
	if(!$post['fee']){
		exi('请输入提现积分','error');
	}
	if(!$post['type']){
		//exi('请选择提现方式','error');
	}
	$fee=$post['fee'];
	if(is_numeric($fee)){
	}else{
		exi('提现积分必须是数字！','error');
	}
	$money=$fee/$setting['jifen_rate'];
	$service_money=round($money*$setting['percent']/100,2);
	if($post['type']==1){
		if(!$post['realname']){
			exi('请输入姓名','error');
		}
		if(!$post['alipay']){
			exi('请输入支付宝帐号','error');
		}
		if(!$post['alipay1']){
			exi('请确认帐号','error');
		}
		if($post['alipay1']!=$post['alipay']){
			exi('确认帐号与支付宝帐号不匹配！','error');
		}
	}
	if($post['type']==2){
		if(!$post['realname']){
			//exi('请输入姓名','error');
		}
		if(!$post['bankname'] && !$post['isapibank']){
			exi('请输入所属银行','error');
		}
		if(!$post['bankcard']){
			exi('请银行卡号','error');
		}
		if(!$post['bankcard1'] && !$post['isapibank']){
			exi('请确认卡号','error');
		}
		if($post['bankcard']!=$post['bankcard1'] && !$post['isapibank']){
			exi('确认卡号与银行卡号不匹配！','error');
		}
	}
	model('core/member/cash')->check_cash_jifen($_SESSION['uid'],$fee,$money);
	$cash=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'ordersn'=>get_ordersn('core_member_cash','ordersn','CASH'),
		'money'=>$money,
		'type'=>intval($post['type']),
		'realname'=>$post['realname'],
		'alipay'=>$post['alipay'],
		'bankname'=>$post['bankname'],
		'bankcard'=>$post['bankcard'],
		'service_money'=>$service_money,
		'remark'=>'积分提现',
		'credit_type'=>1,
		'credit_fee'=>$fee,
		'createtime'=>TIMESTAMP,
		'status'=>0,
	);
	$cash['cash_money']=$cash['money']-$cash['service_money'];
	pdo_insert('core_member_cash',$cash);
	$id=pdo_insertid();
	$type_array=array('积分提现到微信钱包','积分提现到支付宝','积分提现到银行卡');
	$typetitle=$type_array[$cash['type']];
	model('core/member/cash')->notice_tixian($_SESSION['uid'],$money,$typetitle);
	if(model('core/member/cash')->setting['check'] || $params['to_type'] !=0){
		exi('申请提现成功，请等待管理员审核！','','goback');
	}else{
		model('core/member/cash')->pay($id);
		exi('提现成功！','success','goback');
	}
}